<template>
  <div class='container'>
    <el-dialog :title="showTitle" :visible="showDialog" @close="btnCancel">
      <!-- 表单 -->
    <el-form ref="skillForm" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="文章标题" prop="title">
        <el-input v-model="formData.title" style="width:80%"  />
      </el-form-item>
      <el-form-item label="文章内容" prop="articleBody">
        <quill-editor style="width:80%" @blur="onEditorChange($event)" v-model="formData.articleBody"></quill-editor>
      </el-form-item>
      <el-form-item label="视频地址" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" type="textarea" />
      </el-form-item>
    </el-form>
    <!-- 确定和取消 -->
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="8">
        <el-button @click="btnCancel">取消</el-button>
        <el-button type="primary" @click="btnOK">确定</el-button>
      </el-col>
    </el-row>
  </el-dialog>
  </div>
</template>

<script>
import { update, add, detail } from '@/api/hmmm/articles.js'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      // 表单数据
      formData: {
        title: '', // 文章标题
        articleBody: '', // 文章正文
        videoURL: '' // 视频地址
      },

      rules: {
        title: [{ required: true, message: '文章标题不能为空', trigger: 'blur' }],
        articleBody: [{ required: true, message: '文章正文不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    // 当前标题显示内容
    showTitle () {
      return this.formData.id ? '编辑文章' : '新增文章'
    }
  },
  methods: {
    // 富文本校验
    onEditorChange (e) {
      this.$refs.skillForm.validateField('articleBody')
    },
    // 点击确定
    btnOK () {
      this.$refs.skillForm.validate(async isOK => {
        if (isOK) {
          // 表单校验通过
          // 调用接口
          if (this.formData.id) {
            // 如果存在id就是修改
            await update(this.formData)
          } else {
            // 没有id表示新增
            await add(this.formData)
          }
          this.$emit('addDepts')
          this.$emit('update:showDialog', false)
          this.$message.success(`${this.showTitle}成功!`)
        }
      })
    },
    // 点击取消
    btnCancel () {
      // 重置数据
      this.formData = {
        title: '', // 文章标题
        articleBody: '', // 文章正文
        videoURL: '' // 视频地址
      }
      this.$emit('update:showDialog', false)
    },
    // 获取获取详情
    async update (id) {
      const { data } = await detail({ id })
      this.formData = data
    }
  }
}
</script>

<style scoped>

</style>
